﻿Imports System.Windows.Forms
Imports System.Data
Imports System.Data.OracleClient
Imports Barra

Public Class Frm_Ruta
    Dim VectorDeDatos(4) As TextBox
    'conexion para inicializar la barra del navegador
    Dim conexion As String = "Driver={Microsoft ODBC for Oracle};Server=xe;Uid=redes;Pwd=redes;"

    'query para el dgv
    Dim querydgv As String = " select id_ruta as ruta, nombre as nombre, descripcion as descripcion, placa_ as matricula, id_piloto_empleado as piloto from  TB_LOT_RUTA order by id_ruta"
    'llenado del datagridview
    'DgvVehiculo.DataSource=llenarDGV(querydgv)
    'DgvEntrega.DataSource = LlenarDgv(querydgv)
    'variables a incluir si van a llenar un combo
    Dim querycombo As String = "SELECT PLACA_ from TB_LOT_VEHICULO"
    Dim valuecombo As String = "PLACA_" 'valor a insertar en tabla de acuerdo al combo
    Dim displaycombo As String = "TIPO" 'valor a mostrar en el combo

    Dim querycombo1 As String = "SELECT NOMBRE, id_piloto_empleado from TB_LOT_PILOTO_EMPLEADO"
    Dim valuecombo1 As String = "id_piloto_empleado" 'valor a insertar en tabla de acuerdo al combo
    Dim displaycombo1 As String = "NOMBRE" 'valor a mostrar en el combo
    
    'variable para llevar la secuencia en el campo ID
    Dim querysecuencia As String = "select max(ID_RUTA)+ 1 as ID_RUTA from TB_LOT_RUTA"
    Dim camposecuencia As String = "ID_RUTA"

    Public Function LlenarVector() As TextBox()
        VectorDeDatos(0) = TXTRUTA
        VectorDeDatos(1) = TXTNOM
        VectorDeDatos(2) = TXTDES
        VectorDeDatos(3) = TXTPLA
        VectorDeDatos(4) = TXTPIL
        Return VectorDeDatos
    End Function
    Private Sub Frm_Ruta_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        VectorDeDatos = LlenarVector()
        BarraNavegacion1.Inicializar("Driver={Microsoft ODBC for Oracle};Server=xe;Uid=redes; Pwd=redes;", "TB_LOT_RUTA", VectorDeDatos)
        'para llevar la secuencia del codigo
        funciones.llenarcombo(CMBPL, querycombo, valuecombo, displaycombo)
        funciones2.llenarcombo(CMBPI, querycombo1, valuecombo1, displaycombo1)
        'llenado del combo
        Dim temp As String = funciones.traersecuencia(querysecuencia, camposecuencia)
        'llenado del datagridview
        DGVRUTAS.DataSource = funciones.LlenarDgv(querydgv)
        'puse esto de funciones.traersecuencia porque sino me lo tiraba como error entonces llame la clase general primero
        If temp = "" Then
            TXTRUTA.Text = "1"
        Else
            TXTRUTA.Text = temp
        End If
    End Sub

    Private Sub TXTRUTA_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TXTRUTA.TextChanged
        Dim temp As String = funciones.traersecuencia(querysecuencia, camposecuencia)
        If temp = "" Then
            TXTRUTA.Text = "1"
        Else
            TXTRUTA.Text = temp
        End If
    End Sub

   

    
    Private Sub CMBPL_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMBPL.SelectedIndexChanged
        'para que el textbox oculto tome el valor del combo
        If CMBPL.ValueMember.Any Then
            TXTPLA.Text = CMBPL.SelectedValue
        End If
    End Sub

    Private Sub CMBPI_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMBPI.SelectedIndexChanged
        'para que el textbox oculto tome el valor del combo
        If CMBPI.ValueMember.Any Then
            TXTPIL.Text = CMBPI.SelectedValue
        End If
    End Sub


   
End Class
Public Class funciones2
    Public Shared Sub llenarcombo(ByRef combo As System.Windows.Forms.ComboBox, ByVal query As String, ByVal valor As String, ByVal display As String) 'As DataTable
        Dim conexion2 As String = "Server=xe;Uid=redes;Pwd=redes;"
        'Creamos la conexion
        Dim oraCon As New OracleConnection(conexion2)
        'Abrimos la conexion
        oraCon.Open()
        'Creamos un comando
        Dim oraCommand As OracleCommand = New OracleCommand
        'Establecemos la conexion para el comando
        oraCommand.Connection = oraCon
        'Escribimos el texto del comando
        oraCommand.CommandText = query
        'Creamos un objeto adaptador
        Dim oraDataAdapter As New OracleDataAdapter
        oraDataAdapter.SelectCommand = oraCommand
        'Llenamos la tabla
        Dim dttabla As New DataTable
        oraDataAdapter.Fill(dttabla)
        combo.DataSource = dttabla
        'Establecemos el valor a mostrar y el valor a insertar
        combo.ValueMember = valor
        combo.DisplayMember = display
        'Return dttabla
    End Sub
End Class